package com.gxa.shop.servlet.account;

import com.alibaba.fastjson.JSON;
import com.gxa.shop.bean.pojo.Account;
import com.gxa.shop.commons.r.R;

import com.gxa.shop.service.account.AccountService;
import com.gxa.shop.service.account.impl.AccountServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.stream.Collectors;

@WebServlet(name = "UpdateAccount",value = "/account/update")
public class UpdateAccount extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader bufferedReader=req.getReader();
        // 获得数据
        String data=bufferedReader.lines().collect(Collectors.joining());
        // 将数据转换成json对象
        Account account= JSON.parseObject(data, Account.class);
        // 调用service层，实现密码加密
        AccountService service=new AccountServiceImpl();
        int b= service.updateAccount(account);
        // 设置R的空对象
        R r = null;
        if(b>0){
            r=R.ok("更新成功");
        }else {
            R.error("更新失败");
        }
        // 将数据返回给前端
        String jsonResult=JSON.toJSONString(r);
        PrintWriter writer=resp.getWriter();
        writer.write(jsonResult);
        writer.flush();
        writer.close();

    }
}
